package file;

import com.mongodb.client.MongoCollection;
import mongo.MongoTest;
import net.sf.json.JSONObject;
import org.bson.Document;
import util.DateUtil;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日志文件读取
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-04-21 14:29
 */
public class FileReader {
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        readFile("F:\\filet\\default.log.2017-07-17");
        System.out.println(System.currentTimeMillis() - start);
    }

    public static void readFile(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileName), "UTF-8"));
        int i = 0;
        JSONObject obj;
        List<JSONObject> list = new ArrayList<>();
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            i++;
            obj = getJson(line);
            list.add(obj);
            if (list.size() == 50) { // 每50条执行一次插入
//                add(times, list, collection);
            }
        }
        br.close();
    }

    public static JSONObject getJson(String value) {
        String times;
        int i = value.indexOf("{");
        String timeStr = value.substring(0, 23);
        times = value.substring(0, 10);
        if (!DateUtil.isTime(times)) {
            return null;
        }
        value = value.substring(i, value.length());
        JSONObject obj = JSONObject.fromObject(value);
        obj.put("time", timeStr);
        obj.put("topic", "app_hzpt");
        return obj;
    }

    public static boolean read(String s) {
        Pattern p = Pattern.compile("(?<=\\[)(.*?)(?=\\])");
        Matcher matcher = p.matcher(s);
        List<String> list = new ArrayList<>();
//        System.out.println(s);
        System.out.print(matcher.groupCount() + " : ");
        for (int j = 0; j < 3; j++) {
            if (matcher.find()) {
                System.out.print(matcher.group() + " ");
                list.add(matcher.group());
            } else {
                return false;
            }
        }
        JSONObject obj = new JSONObject();
//        obj.put("ip", list.get(0));
//        obj.put("time", list.get(1));
//        obj.put("request", list.get(2));
//        obj.put("status", list.get(3));
//        obj.put("cost_time", list.get(4));
//        obj.put("upstream_addr", list.get(5));
//        obj.put("http_user_agent", list.get(6));
        return true;
    }

    /**
     * 下载文件到本地
     *
     * @param urlString 被下载的文件地址
     * @param filename 本地文件名
     * @throws Exception 各种异常
     */
    public static void download(String urlString, String filename) throws Exception {
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        int contentLength = con.getContentLength();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        // 1K的数据缓冲
        byte[] bs = new byte[1024];

        // 读取到的数据长度
        // 输出的文件流
        BufferedWriter os = new BufferedWriter(new FileWriter(filename));
        int i = 0;
        // 开始读取
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            os.write(line);
            System.out.println(i++);
        }
        // 完毕，关闭所有链接
        os.close();
        br.close();
    }
}
